﻿
/*
Contains functions that override default asp.net validator functions for enhanced UI experience.
*/

ValidatorUpdateIsValid = function() {
    Page_IsValid = AllValidatorsValid(Page_Validators);
    ClearValidatorCallouts();
    SetValidatorCallouts();
}

ValidatorValidate = function(val, validationGroup, event) {
    val.isvalid = true;
    if ((typeof (val.enabled) == 'undefined' || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
        if (typeof (val.evaluationfunction) == 'function') {
            val.isvalid = val.evaluationfunction(val);
            if (!val.isvalid && Page_InvalidControlToBeFocused == null &&
                typeof (val.focusOnError) == 'string' && val.focusOnError == 't') {
                ValidatorSetFocus(val, event);
            }
        }
    }

    ClearValidatorCallouts();
    SetValidatorCallouts();

    ValidatorUpdateDisplay(val);
}

SetValidatorCallouts = function() {
    var i;
    var pageValid = true;
    var validationGroup = '';

    for (i = 0; i < Page_Validators.length; i++) {

        var inputControl = document.getElementById(Page_Validators[i].controltovalidate);
        var cssCampoError = document.getElementById(Page_Validators[i].id).getAttribute('CssCampoErro');

        if (!Page_Validators[i].isvalid) {
            if (pageValid) {
                try {
                    inputControl.focus();
                    if (Page_Validators[i].validationGroup != null && Page_Validators[i].validationGroup != '')
                        validationGroup = Page_Validators[i].validationGroup;
                }
                catch (e) {
                }
            }

            if (cssCampoError != null)
                WebForm_AppendToClassName(inputControl, cssCampoError);

            var labelID = document.getElementById(Page_Validators[i].id).getAttribute('LabelID');

            if (labelID != null) {
                var labelControl = document.getElementById(labelID);

                if (labelControl != null) {
                    var labelCssError = document.getElementById(Page_Validators[i].id).getAttribute('CssLabelErro');
                    $(labelID).addClass(labelCssError);
                    WebForm_AppendToClassName(labelControl, labelCssError);
                }
            }

            pageValid = false;
        }
    }

    if (!pageValid && typeof (Page_ValidationSummaries) != "undefined") {
        for (i = 0; i < Page_ValidationSummaries.length; i++) {
            if (validationGroup == '' ||
					(Page_ValidationSummaries[i].validationGroup != null &&
					 Page_ValidationSummaries[i].validationGroup == validationGroup)
			   ) {
                Page_ValidationSummaries[i].style.display = '';
                Page_ValidationSummaries[i].focus();
            }
        }
    }

    return pageValid;
}

ClearValidatorCallouts = function() {
    var i;
    var invalidConrols = [];
    for (i = 0; i < Page_Validators.length; i++) {
        var inputControl = document.getElementById(Page_Validators[i].controltovalidate);
        var cssCampoError = document.getElementById(Page_Validators[i].id).getAttribute('CssCampoErro');

        if (cssCampoError != null)
            WebForm_RemoveClassName(inputControl, cssCampoError);

        var labelID = document.getElementById(Page_Validators[i].id).getAttribute('LabelID');

        if (labelID != null) {
            var labelControl = document.getElementById(labelID);

            if (labelControl != null) {
                var labelCssError = document.getElementById(Page_Validators[i].id).getAttribute('CssLabelErro');
                $(labelID).removeClass(labelCssError);
                WebForm_RemoveClassName(labelControl, labelCssError);
            }
        }
    }
}

ValidatorOnChange = function(event) {
    if (!event) {
        event = window.event;
    }
    Page_InvalidControlToBeFocused = null;
    var targetedControl;
    if ((typeof (event.srcElement) != "undefined") && (event.srcElement != null)) {
        targetedControl = event.srcElement;
    }
    else {
        targetedControl = event.target;
    }
    var vals;

    if (typeof (targetedControl.Validators) != "undefined") {
        vals = targetedControl.Validators;
        // alert('here - 1');
    }
    else {
        if (targetedControl.tagName.toLowerCase() == "label") {
            targetedControl = document.getElementById(targetedControl.htmlFor);
            vals = targetedControl.Validators;
        }
    }
    var i;

    //if date picker caused it in IE...which can't find the textbox to get the list of validators for the control, so revalidate whole page
    if (vals == null)
        vals = Page_Validators;

    for (i = 0; i < vals.length; i++) {
        ValidatorValidate(vals[i], null, event);
    }

    ValidatorUpdateIsValid();
}

ValidatorUpdateDisplay = function(val) {
    if (typeof (val.display) == "string") {
        if (val.display == "None") {
            return;
        }
        if (val.display == "Dynamic") {
            //changed this block to not set display to inline, but to remove style attribute entirely
            //undo comments if not using jQuery
            if (val.isvalid)
                $(val).hide("fast"); //val.style.display = "none"; 
            else
                $(val).show("fast"); //val.removeAttribute("style");
            return;
        }
    }
    if ((navigator.userAgent.indexOf("Mac") > -1) && (navigator.userAgent.indexOf("MSIE") > -1)) {
        val.style.display = "inline";
    }
    val.style.visibility = val.isvalid ? "hidden" : "visible";
}

function ValidationSummaryOnSubmit(validationGroup) {
    if (typeof (Page_ValidationSummaries) == "undefined")
        return;
    var summary, sums, s;
    for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
        summary = Page_ValidationSummaries[sums];
        summary.style.display = "none";

        var modalID = summary.getAttribute('ModalID');
        var textID = summary.getAttribute('TextID');

        if (!Page_IsValid &&
                (IsValidationGroupMatch(summary, validationGroup) || (textID != null && modalID != null))
            ) {
            var i;
            if (summary.showsummary != "False") {
                summary.style.display = "";
                if (typeof (summary.displaymode) != "string") {
                    summary.displaymode = "BulletList";
                }
                switch (summary.displaymode) {
                    case "List":
                        headerSep = "<br>";
                        first = "";
                        pre = "";
                        post = "<br>";
                        end = "";
                        break;
                    case "BulletList":
                    default:
                        headerSep = "";
                        first = "<ul>";
                        pre = "<li>";
                        post = "</li>";
                        end = "</ul>";
                        break;
                    case "SingleParagraph":
                        headerSep = " ";
                        first = "";
                        pre = "";
                        post = " ";
                        end = "<br>";
                        break;
                }
                s = "";
                if (typeof (summary.headertext) == "string") {
                    s += summary.headertext + headerSep;
                }
                s += first;
                for (i = 0; i < Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid && typeof (Page_Validators[i].errormessage) == "string") {
                        s += pre + Page_Validators[i].errormessage + post;
                    }
                }
                s += end;
                if (textID != null && modalID != null) {
                    AbrirPopupModalMSG(textID, modalID, s);
                }
                else {
                    summary.innerHTML = s;
                    var position = Sys.UI.DomElement.getLocation(summary);
                    window.scrollTo(0,position.y);
                }
            }
            if (summary.showmessagebox == "True") {
                s = "";
                if (typeof (summary.headertext) == "string") {
                    s += summary.headertext + "\r\n";
                }
                var lastValIndex = Page_Validators.length - 1;
                for (i = 0; i <= lastValIndex; i++) {
                    if (!Page_Validators[i].isvalid && typeof (Page_Validators[i].errormessage) == "string") {
                        switch (summary.displaymode) {
                            case "List":
                                s += Page_Validators[i].errormessage;
                                if (i < lastValIndex) {
                                    s += "\r\n";
                                }
                                break;
                            case "BulletList":
                            default:
                                s += "- " + Page_Validators[i].errormessage;
                                if (i < lastValIndex) {
                                    s += "\r\n";
                                }
                                break;
                            case "SingleParagraph":
                                s += Page_Validators[i].errormessage + " ";
                                break;
                        }
                    }
                }
                alert(s);
            }
        }
    }
}

function AbrirPopupModalMSG(textID, modalID, texto)
{
	$('#' + textID).html(texto);
        $find(modalID).show();
}